﻿using System;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Services.Tools
{
	internal class OperationInspector : BusinessBase, IDispatchMessageInspector, IServiceBehavior
	{
		static OperationInspector()
		{
		}

		#region IServiceBehavior Members

		public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
		{
			return;
		}

		public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
		{
			foreach (ChannelDispatcher chDispatcher in serviceHostBase.ChannelDispatchers)
			{
				foreach (EndpointDispatcher epDispatcher in chDispatcher.Endpoints)
				{
					epDispatcher.DispatchRuntime.MessageInspectors.Add(this);
				}
			}
		}

		public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
		{
			return;
		}

		#endregion

		#region IDispatchMessageInspector Members

		public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
		{
			return Guid.Empty;
		}

		public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
		{
		}

		#endregion
	}

	public class OperationInspectorBehaviorExtension : BehaviorExtensionElement
	{
		public override Type BehaviorType
		{
			get { return typeof(OperationInspector); }
		}

		protected override object CreateBehavior()
		{
			return new OperationInspector();
		}
	}


}
